package com.fortune.base;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fortune.common.redis.RedisClient;
import com.fortune.constants.redis.CacheKeys;

public abstract class BaseController {
	@Autowired
	private RedisClient redisClient;
	/**
	 * 获取登录用户的用户id
	 * @return
	 */
	protected String getUserId() {
		String[] ids = getIds();
		return ids[0];
	}
	/**
	 * 获取登录用户所属的厂商id
	 * @return
	 */
	protected String getMId() {
		String ids = "";
		return ids;
	}
	/**
	 * 获取登录用户所属的经销商id
	 * @return
	 */
	protected String getDId() {
		String[] ids = getIds();
		return ids[1];
	}
	/**
	 * 获取登录用户的手机号
	 * @return
	 */
	protected String getPhone() {
		String[] ids = getIds();
		return ids[2];
	}
	
	private String[] getIds() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String token = request.getHeader("Authorization");
		String idString = redisClient.get(CacheKeys.SALES_MAN_LOGIN_TOKEN, token);
		String[] ids = idString.split(",");
		return ids;
	}
}
